package org.hsc.web;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hsc.common.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class PrivilegeInteceptor implements HandlerInterceptor {

	private final static Logger log=LoggerFactory.getLogger(PrivilegeInteceptor.class);
	
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
        String uri = request.getRequestURI();
        log.debug("收到请求："+uri);
        request.setAttribute("URI", uri);//用于分页处理；
        //后台session控制
        if (uri.contains("login.do")||uri.contains("user/generateValidateCode.do")) {  
        	return true; 
        }
        else{
        	return true; 
//        	Object obj=request.getSession().getAttribute("managerHasLogin");
//        	if (obj!=null) {
//	        	return true;
//        	}else {
//        		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
//        		if("true".equals(request.getParameter("ajax"))){
//        			response.setStatus(401);
//	        		ReturnResult rst=ReturnResult.FAILUER("用户未登陆！",basePath);
//	        		response.setContentType("text/html;charset=UTF-8");           
//					response.setHeader("Cache-Control", "no-cache");
//	        		PrintWriter  writer=response.getWriter();
//	        		writer.write(JsonUtils.toJson(rst));
//        		}else{
//        			log.debug("会话未登录！跳转登录页面：");
//        			response.sendRedirect(basePath);
//        		}
//        		return false;
//        	}
        }
	}
}